﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using kegg_service;
using Excel = Microsoft.Office.Interop.Excel;
using System.Net;
using System.Xml;
using System.Runtime.InteropServices;
using System.Diagnostics;
using QuickGraph;
using QuickGraph.Algorithms;
using System.Runtime.Serialization.Formatters.Binary;
using MySql.Data.MySqlClient;



namespace BPP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


            ShowSelectedPath f = new ShowSelectedPath();
            f.Show();


        }


        private void button1_Click(object sender, EventArgs e)
        {
            _programm.read_model.listOfMetabolites.Clear();
            _programm.read_model.listOfReactions.Clear();
 
            DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result == DialogResult.OK) // Test result.
            {

                string file = openFileDialog1.FileName;
                _programm.shared_functions._path = file;

                try
                {
                   _programm.read_model.ReadExcelModel(file);

                    
                   foreach (_programm.Metabolites m in _programm.read_model.listOfMetabolites)
                        comboBox2.Items.Add(m.KEGG_ID + " " + m.Name);


                  


                    string[] lines = System.IO.File.ReadAllLines("compounds.txt");
                    foreach (string s in lines)
                    {
                        comboBox1.Items.Add(s.Replace("\t", " "));
                    }

                    comboBox1.SelectedIndex = 0;
                    comboBox2.SelectedIndex = 0;

                    label4.Text = "List of metabolites loaded!";

                    BidirectionalGraph<object, IEdge<object>> paths = new BidirectionalGraph<object, IEdge<object>>();

                    _programm.read_model.CreateGraphToVisualize(_programm.read_model.listOfReactions);




                    label4.Text = "graph created";

                   
                    //zapisj v fail
                    byte[] graph_to_visualize = _programm.shared_functions.ObjectToByteArray((object)_programm.shared_functions.model_graph);
                    File.WriteAllBytes("graph.txt", graph_to_visualize);

                    label4.Text = "finish";

                    //button1.Enabled = true;
                    
                    
                }
                catch (IOException)
                {
                }
            }
        }

       

        private void button2_Click(object sender, EventArgs e)
        {

            if (_programm.read_model.listOfReactions.Count != 0)
            {

                lstBoxPaths.Items.Clear();

                _programm.pathway_search.FindReactions("cpd:" + comboBox1.SelectedItem.ToString().Substring(0, 6),
                    "cpd:" + comboBox2.SelectedItem.ToString().Substring(0, 6));


                //zapisj v fail

                //     controller.shared_functions.constructed_graph.AddVertex(controller.shared_functions.model_graph);

                byte[] graph_to_visualize1 = _programm.shared_functions.ObjectToByteArray((object)_programm.shared_functions.constructed_graph);
                File.WriteAllBytes("graph1.txt", graph_to_visualize1);


            }
            else
            {
                MessageBox.Show("Model is not loaded!");
            }

        }



      

        private void button3_Click(object sender, EventArgs e)
        {
            lstBoxPaths.Items.Clear();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try {
                
                txtPatwayCount.Text = (int.Parse(txtPatwayCount.Text) + 1).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("'max patways count' is not correct");
            }

            
        }


        /// <summary>
        /// TODO esli menjwe 0 - skazatj cto neljzja menjwe nulja cifru ili menjwe 1
        /// esli sliwkom boljwoe cislo - boljwe ce,m levek, to pokazivatj soobwenie (3 levels -max treshhoold 5), min 3, levle 4 max trewhol 7
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>


        private void button4_Click(object sender, EventArgs e)
        {
           try
            {

                txtPatwayCount.Text = (int.Parse(txtPatwayCount.Text) - 1).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("'max patways count' is not correct");
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {

                txtLevel.Text = (int.Parse(txtLevel.Text) + 1).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("'max patways count' is not correct");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {

                txtLevel.Text = (int.Parse(txtLevel.Text) - 1).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("'max patways count' is not correct");
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            try
            {

                txtTreshhold.Text = (int.Parse(txtTreshhold.Text) + 2).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("'max patways count' is not correct");
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                txtTreshhold.Text = (int.Parse(txtTreshhold.Text) - 2).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("'max patways count' is not correct");
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            this.Close();
        }




        private void button11_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;

            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();

            xlWorkBook =
                xlApp.Workbooks.Open(_programm.shared_functions._path, 0, false,5, "", "",
                    false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

       
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel.Range range = xlWorkSheet.UsedRange;
            (range.Cells[1, 16] as Excel.Range).Value ="";

            //add selected to model
        }

  
      
    
    }

    
}
